// index.ts
// 获取应用实例
const app = getApp<IAppOption>()

Page({
    data: {
        red: 0, //红灯开
        green: 0, //
        blue: 0, //
        results: [] //数据流
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs',
        })
    },
    onLoad() {
        let _this = this;
        setInterval(function () {
            _this.getDataStreamsRecently();
        }, 2000)
    },
    //获取数据流
    getDataStreamsRecently() {
        let _this = this;
        wx.request({
            url: 'http://101.43.140.38:8081//datastream/multi?devId=1102134133&dataStramIds=Tempreture,Humidity',
            data: {},
            header: {},
            success(res) {
                _this.setData({
                    results: res.data.result
                })
                console.log('data = ' + JSON.stringify(_this.data));
            }
        })
    },
    redChange(e: any) {
        if (e.detail.value == true) {
            this.ledcmd(1);
        } else {
            this.ledcmd(0);
        }
    },
    greenChange(e: any) {
        if (e.detail.value == true) {
            this.ledcmd(2);
        } else {
            this.ledcmd(0);
        }
    },
    blueChange(e: any) {
        if (e.detail.value == true) {
            this.ledcmd(3);
        } else {
            this.ledcmd(0);
        }
    },
    //1 打开 0关闭
    ledcmd(e: number) {
        let _this = this;
        let url = "http://101.43.140.38:8081/cmd/send?devId=1102134133&cmd=" + e;
        console.log('url = ' + url);
        wx.request({
            url: url,
            data: {},
            header: {},
            success(res) {
                //console.log('led cmd res: ' +JSON.stringify(res));
            }

        })
    }
})
